{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Quantum State</h2>\n",
    "\n",
    "The overall probability must be 1 when we observe a quantum system.\n",
    "\n",
    "For example, the following vectors <u>cannot</u> be a valid quantum state:\n",
    "\n",
    "$$\n",
    "    \\myvector{ \\frac{1}{2} \\\\ \\frac{1}{2} } \n",
    "    \\mbox{ and }\n",
    "    \\myvector{ \\frac{\\sqrt{3}}{2} \\\\ \\frac{1}{\\sqrt{2}} }.\n",
    "$$\n",
    "\n",
    "For the first vector: the probabilities of observing the states $\\ket{0} $ and $ \\ket{1} $ are $ \\frac{1}{4} $. \n",
    "\n",
    "So, the overall probability of getting a result is $ \\frac{1}{4} + \\frac{1}{4} = \\frac{1}{2} $, which is less than 1.\n",
    "\n",
    "For the second vector: the probabilities of observing the states $\\ket{0} $ and $ \\ket{1} $ are respectively $ \\frac{3}{4} $ and $ \\frac{1}{2} $. \n",
    "\n",
    "So, the overall probability of getting a result is $ \\frac{3}{4} + \\frac{1}{2} = \\frac{5}{4} $, which is greater than 1."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"blue\"><b>The summation of amplitude squares must be 1 for a valid quantum state.</b></font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=\"blue\"><b>In other words, a quantum state can be represented by a vector having length 1, and vice versa.</b></font>\n",
    "\n",
    "<i>The summation of amplitude squares gives the square of the length of vector.\n",
    "\n",
    "But, this summation is 1, and its sqaure root is also 1. So, we directly use the term <u>length</u> in the defintion.</i>\n",
    "\n",
    "We represent a quantum state as $ \\ket{u} $ instead of $ u $.\n",
    "\n",
    "Remember the relation between the length and inner product: $ \\norm{u} = \\sqrt{\\inner{u}{u}} $.\n",
    "\n",
    "In quantum computation, we use almost the same notation for the inner product: $ \\braket{u}{u}$.\n",
    "\n",
    "$ \\norm{ \\ket{u} } = \\sqrt{ \\braket{u}{u} } = 1 $, or equivalently $ \\braket{u}{u} = 1 $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Let $a$ and $b$ be real numbers.\n",
    "\n",
    "If the folllowing vectors are valid quantum states, then what can be the values of $a$ and $b$?\n",
    "\n",
    "$$\n",
    "    \\ket{v} = \\myrvector{a \\\\ -0.1 \\\\ -0.3 \\\\ 0.4 \\\\ 0.5}\n",
    "    ~~~~~ \\mbox{and} ~~~~~\n",
    "   \\ket{u} = \\myrvector{ \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{b}} \\\\ -\\frac{1}{\\sqrt{3}} }.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your code is here or you may find the values by hand (in mind)\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B46_Quantum_State_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Quantum Operators </h3>\n",
    "\n",
    "Once the quantum state is defined, the definition of quantum operator is very easy.\n",
    "\n",
    "<font color=\"blue\"><b>Any length preserving matrix is a quantum operator, and vice versa.</b></font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2</h3>\n",
    "\n",
    "Remember Hadamard operator:\n",
    "\n",
    "$$\n",
    "    H = \\hadamard.\n",
    "$$\n",
    "\n",
    "Let's randomly create a 2-dimensional quantum state, and test whether Hadamard operator preserves the length or not.\n",
    "\n",
    "Write a function that returns a randomly created 2-dimensional quantum state:\n",
    "<ul>\n",
    "    <li> Pick a random value between 0 and 100 </li>\n",
    "    <li> Divide it by 100</li>\n",
    "    <li> Take sqaure root of it</li>\n",
    "    <li> Randomly determine its sign ($+$ or $-$)</li>\n",
    "    <li> This is the first entry of the vector </li>\n",
    "    <li> Find an appropriate value for the second entry </li>\n",
    "    <li> Randomly determine its sign ($+$ or $-$)</li>\n",
    "</ul>\n",
    "\n",
    "Write a function that determines whether a given vector is a valid quantum state or not.\n",
    "\n",
    "(Due to precision problem, the summation of squares may not be exactly 1 but very close to 1, e.g., 0.9999999999999998.)\n",
    "\n",
    "Repeat 10 times:\n",
    "<ul>\n",
    "    <li> Randomly create a quantum state </li>\n",
    "    <li> Multiply Hadamard matrix with the randomly created quantum state </li>\n",
    "    <li> Check whether the result quantum state is valid </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B46_Quantum_State_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
